#include <czmq.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <assert.h>
#include "bsm_msgs.pb-c.h"
#define MAX_MSG_SIZE 1024

int main (void)
{
    BSMMulti *bsm_multi;

    //connect to server
    void *context = zmq_ctx_new ();
    void *responder = zmq_socket (context, ZMQ_REP);
    int rc = zmq_bind (responder, "tcp://*:5555");
    assert (rc == 0);

    while (1) {
        //allocate a data
        uint8_t buf[MAX_MSG_SIZE];
        size_t msg_len = zmq_recv (responder, buf, MAX_MSG_SIZE, 0);

        //unpack bsm_multi data
        bsm_multi = bsm_multi__unpack(NULL, msg_len, buf);
        if (bsm_multi == NULL){
            fprintf(stderr, "error unpacking incoming message\n");
            exit(1);
        }

        //print bsm_multi data
        for(int i = 0; i < bsm_multi -> size; i++){
            BSM *bsm = bsm_multi->data[i];
            printf("Received: idbuf = %d \n",bsm->idbuf);
            bsm__free_unpacked(bsm, NULL);
        }
        
        //do something here
        sleep (1);

        //return a responce
        zmq_send (responder, "World", 5, 0);

    }
    //free the data
    bsm_multi__free_unpacked(bsm_multi, NULL);
    return 0;
}